//
// Created by FindBin on 2024/4/28.
//

#include "rs485.h"

#if defined(USE_RS485_1)
uint8_t rs485_rx_Buffer_1[RS485_BUFFER_SIZE_1] = {0};
#endif
#if defined(USE_RS485_2)
uint8_t rs485_rx_Buffer_2[RS485_BUFFER_SIZE_2] = {0};
#endif
#if (defined(USE_RS485_1) | defined(USE_RS485_2))

void rs485_init() {
#if defined(USE_RS485_1)
    HAL_GPIO_WritePin(RS485_1_DE_port, RS485_1_DE_pin, 0);
    HAL_UART_Receive_IT(&RS485_1_UART, rs485_rx_Buffer_1, RS485_BUFFER_SIZE_1);
#endif
#if defined(USE_RS485_2)
    HAL_GPIO_WritePin(RS485_2_DE_port, RS485_2_DE_pin, 0);
    HAL_UART_Receive_IT(&RS485_2_UART, rs485_rx_Buffer_2, RS485_BUFFER_SIZE_2);
#endif
}

#endif
#if defined(USE_RS485_1)

void rs485_1_send_msg(uint8_t *pTxData, uint16_t len) {
    HAL_GPIO_WritePin(RS485_1_DE_port, RS485_1_DE_pin, 1);
    HAL_UART_Transmit(&RS485_1_UART, pTxData, len, HAL_MAX_DELAY);
    HAL_GPIO_WritePin(RS485_1_DE_port, RS485_1_DE_pin, 0);
}

#endif

#if defined(USE_RS485_2)

void rs485_2_send_msg(uint8_t *pTxData, uint16_t len) {
    HAL_GPIO_WritePin(RS485_2_DE_port, RS485_2_DE_pin, 1);
    HAL_UART_Transmit(&RS485_2_UART, pTxData, len, HAL_MAX_DELAY);
    HAL_GPIO_WritePin(RS485_2_DE_port, RS485_2_DE_pin, 0);
}

#endif
#if (defined(USE_RS485_1) | defined(USE_RS485_2))

inline void rs485_callback() {
#if defined(USE_RS485_1)
    HAL_UART_Receive_IT(&RS485_1_UART, rs485_rx_Buffer_1, RS485_BUFFER_SIZE_1);
#endif

#if defined(USE_RS485_2)
    HAL_UART_Receive_IT(&RS485_2_UART, rs485_rx_Buffer_2, RS485_BUFFER_SIZE_2);
#endif
}

#endif


